App.controller('shelterController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {

    $scope.grid = {
        options : {
            bAutoWidth : false,
            scrollX : true,
            fixedColumns: {
                leftColumns : 5
            }
        },
        add : function() {
            $scope.shelter = {};
            $scope.modal.show();
        },
        update: function(row) {
            $scope.shelter = row;
            $scope.modal.show();
        },
        save : function() {
            $scope.validateForm.doValidate();
            if($scope.validateForm.$valid) {
                Page.ajaxPost($scope.shelter.shelterId ? 'shelter/update.do' : 'shelter/add.do', $scope.shelter, function() {
                    Page.notice("操作成功!");
                    $scope.grid.query();
                    $scope.grid.cancel();
                });
            }
        },
        viewStopPosition : function(row) {
            $scope.tempShelter = row;
            $scope.stopPointsModal.show();
        },
        adHistory : function(row) {
            Page.notice("待定!");
            //Page.ajaxPost('bus/adHistory.do', {busId : row.busId}, function (resp) {
            //    $scope.historyList = resp.data;
            //    $scope.adHistoryModal.show();
            //});
        },
        column_defs: [
            {
                target : 1,
                template : function(row) {
                    return '<a href="javascript:void(0)" ed-click="adHistory">'+row.shelterNo+'</a>'
                        + '<a href="javascript:void(0);" ed-click="viewStopPosition" title="定位" class="icon-pointer" style="padding-left: 10px;font-size:13px;"></a>'
                }
            },
            {
                target : 17,
                buttons : [
                    {
                        name: '编辑',
                        class: 'btn-warning btm-sm',
                        icon: 'fa-edit',
                        event: 'update'
                    },
                    {
                        name: '删除',
                        class: 'btn-danger',
                        icon: 'fa-close',
                        event: function(row) {
                            Page.confirm("确定要删除吗？", function() {
                                Page.ajaxPost('shelter/delete.do', row, function() {
                                    $scope.grid.query();
                                });
                            });
                        }
                    },
                    {
                        name: '广告历史',
                        class: 'btn btn-xs blue',
                        icon: 'glyphicon glyphicon-tag',
                        event: 'adHistory'
                    }
                ]
            }]
    };


    $scope.cancelAdHistoryModal = function() {
        $scope.adHistoryModal.hide();
    };

    $scope.stopPointsModal = {
        onShow : function() {
            var mapDiv = "stopPointsModalMapDiv";
            var width = $('#' + mapDiv).width();
            var height = $('#' + mapDiv).height();
            $scope.stopPointsModal.map = new AMap.Map(mapDiv, {resizeEnable: true,zoom:13});

            var markers = [];
            var infoWindow = new AMap.InfoWindow({offset:new AMap.Pixel(0,-30)});
            if($scope.tempShelter.longitude && $scope.tempShelter.latitude){
                var marker = new AMap.Marker({
                    position: [$scope.tempShelter.longitude, $scope.tempShelter.latitude],
                    title:$scope.tempShelter.stopName + '-(' + $scope.tempShelter.shelterNo + '-' + $scope.tempShelter.direction + ')',
                    map:$scope.stopPointsModal.map
                });
                marker.on('click',function(e){
                    infoWindow.setContent(e.target.getTitle());
                    infoWindow.open($scope.stopPointsModal.map, e.target.getPosition());
                });
                markers[markers.length] = marker;
            }else{
            }

            setTimeout(function() {
                if(markers.length > 1){
                    $scope.stopPointsModal.map.setFitView();
                }else if(markers.length == 1){
                    $scope.stopPointsModal.map.setCenter(markers[0].getPosition());
                }
            },300);

        }
    }


}]);